# $Id: scan.dnsbl.tcl 18 2012-01-06 23:56:32Z nickoos85@gmail.com $

package require dns

set blsb(sbl) {
        "dnsbl.tornevall.org"
        "dnsbl.dronebl.org"
}

proc altera:check_dnsbl { dns ip } {
  regexp {([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})} $ip matches class1 class2 class3 class4
  if {[info exists class1] && [info exists class2] && [info exists class3] && [info exists class4]} {
        set ia [dns::resolve $class4.$class3.$class2.$class1.$dns]
    set ib [dns::status $ia]
    dns::cleanup $ia
    if {$ib eq "ok"} {return 1} else {return 0}
  } else {return 0}
}

proc altera:score_dnsbl {uid} {
	set ::punished($uid) 0
	set stop 0
	set stop2 0
	set total 0; set details [list]
	set bl [lindex $::blsb(sbl) 0]
	foreach dns $::blsb(sbl) {
		if {$::punished($uid) == 0 && [altera:check_dnsbl $dns $::ip($uid)]} {
			switch -exact $::altera(action) {
				"0" { }
				"1" { altera:sendRaw ":$::altera(uid) KILL *@$uid :Dnsbl: You are listed on $dns - Please check http://dnsbl.tornevall.org/scan.php for more information" }
				"2" { altera:sendRaw ":$::altera(uid) GLINE *@$::ip($uid) 1h :Dnsbl: You are listed on $dns - Please check http://dnsbl.tornevall.org/scan.php for more information" }
			}
			catch {open $::DIR/data/blacklist.dat r} liste2
			while {![eof $liste2]} { 
				gets $liste2 verif2; 
					if {![string compare -nocase $::ip($uid) $verif2]} { 
					set stop2 "1"
				}
			}
			catch {close $liste2}; 
			if {$stop2=="1"} { 
			} else {
				set blacklist [open $::DIR/data/blacklist.dat a]; puts $blacklist [encoding convertto iso8859-1 $::ip($uid)]; close $blacklist
			}
			altera:sendRaw ":$::altera(uid) PRIVMSG $::altera(salon) :\[DNSBL\] $::nick($uid) ($::ip($uid)) listed on $dns"
			set stop 1
			set ::punished($uid) 1
		}
	} 
	if {$stop == "1"} { } else { scan:port $uid }
}


